package Modelo.Controler;



/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



import Model.Beans.Book;
import Model.Beans.Institution;
import Model.Beans.User;
import Model.DAO.InstitutionDAO;
import Model.Servicios.BookSrv;
import Model.Utilities.Constantes.BookState;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;

/**
 *
 * @author sixviv
 */
public class InstBooksListsAction extends ActionSupport implements SessionAware {
    private BookSrv bSrv = new BookSrv();
    private InstitutionDAO  iDAO = new InstitutionDAO();
    private Map<String, Object> mapSession;
    private List<Book> books;
    private String id;
    private Book book;
    private boolean pendientes = false;
    private boolean solicitados = false;
    private String type;
    private User user;

    public String get(){
        String respuesta="";
        user = (User) mapSession.get("user");
        setBooks(bSrv.getBooks(user));
        respuesta = "success_get";
        return respuesta;

    }

    public String getSol(){
        String respuesta="";
        user = (User) mapSession.get("user");
        setBooks(bSrv.getBooks(user));
        setBooks(bSrv.getBooksSol(user));
        setSolicitados(true);
        respuesta = "success_get";
        return respuesta;

    }

    public String getPen(){
        String respuesta="";
        user = (User) mapSession.get("user");
        setBooks(bSrv.getBooks(user));
        setBooks(bSrv.getBooksPen(user));
        pendientes = true;
        respuesta = "success_get";
        return respuesta;

    }

    public String aprobar(){
        book = bSrv.getBook(Integer.valueOf(id));
        book.setStatus(BookState.APROBADO);
        bSrv.update(book);
        addActionMessage("LA SOLICITUD HA SIDO APROBADA");
        if(type!=null){
            if(type.compareTo(BookState.PENDIENTE)==0){
                return getPen();
            }else{
                if(type.compareTo(BookState.SOLICITADO)==0){
                    return getSol();
                }else{
                    return "error";
                }
            }
        }else{
            return "error";
        }
    }

    public String rechazar(){
        book = bSrv.getBook(Integer.valueOf(id));
        bSrv.delete(book);
        addActionMessage("LA SOLICITUD HA SIDO ELIMINADA");
        if(type!=null){
            if(type.compareTo(BookState.PENDIENTE)==0){
                return getPen();
            }else{
                if(type.compareTo(BookState.SOLICITADO)==0){
                    return getSol();
                }else{
                    return "error";
                }
            }
        }else{
            return "error";
        }
    }

    public String verDetalles(){
        setBook(bSrv.getBook(Integer.valueOf(id)));
        if(type!=null){
            if(type.compareTo(BookState.PENDIENTE)==0)
                pendientes = true;
            if(type.compareTo(BookState.SOLICITADO)==0)
                solicitados = true;
        }
        return "verDetalles";
    }
    
    public void setSession(Map<String, Object> arg0) {
        this.mapSession = arg0;
    }

    /**
     * @return the books
     */
    public List<Book> getBooks() {
        return books;
    }

    /**
     * @param books the books to set
     */
    public void setBooks(List<Book> books) {
        this.books = books;
    }

    /**
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * @return the book
     */
    public Book getBook() {
        return book;
    }

    /**
     * @param book the book to set
     */
    public void setBook(Book book) {
        this.book = book;
    }

    /**
     * @return the pendientes
     */
    public boolean isPendientes() {
        return pendientes;
    }

    /**
     * @param pendientes the pendientes to set
     */
    public void setPendientes(boolean pendientes) {
        this.pendientes = pendientes;
    }

    /**
     * @return the solicitados
     */
    public boolean isSolicitados() {
        return solicitados;
    }

    /**
     * @param solicitados the solicitados to set
     */
    public void setSolicitados(boolean solicitados) {
        this.solicitados = solicitados;
    }

    /**
     * @return the type
     */
    public String getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(String type) {
        this.type = type;
    }

    public void setUsuario(User usuario) {
        this.user = usuario;
    }

}
